# -*- coding: utf-8 -*-

'''eof
name:企业当前被列入黑名单
code:R21_EntBlackListCheck_WHB
tableName:
columnName:
groups:弱规则>黑名单信息校验
dependencies:WDXT_Black
type:常用指标
datasourceType:在线指标
description:
eof'''


import sys
import pandas as pd
import json
from datetime import datetime


reload(sys)
sys.setdefaultencoding('utf-8')
null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']


def checkBlack(base, baseName, blackList):
    if base in null_type_list:
        return "获取到的数据为空"
    try:
        code = base["sysHead"]["responseCode"]
    except:
        return "接口获取失败"
    if code == "0":
        try:
            blackInfo = base.get("body").get(baseName)
        except:
            return "接口获取失败"
        if blackInfo not in null_type_list and isinstance(blackInfo, list):
            if len(blackInfo) >= 1:
                flag = False
                for black in blackInfo:
                    certId = black.get("CERTID")
                    if certId not in null_type_list and certId in blackList:
                        return 0
                    elif certId in null_type_list:
                        flag = True
                if flag:
                    return "接口数据异常"
                return 1
            else:
                return 1
        else:
            return "接口数据异常"
    else:
        return "接口获取失败"


def getRule21():
    certIdList = []
    creditCode = ratingRequest.get("creditCode")
    regNumber = ratingRequest.get("regNumber")
    entTaxID = ratingRequest.get("entTaxID")
    extOrgCode = ratingRequest.get("extOrgCode")
    if creditCode not in null_type_list:
        certIdList.append(creditCode)
    if extOrgCode not in null_type_list:
        certIdList.append(extOrgCode)
    if regNumber not in null_type_list:
        certIdList.append(regNumber)
    if entTaxID not in null_type_list:
        certIdList.append(entTaxID)
    else:
        raise RuntimeError("纳税识别号为空，无法判断企业黑名单")
    if len(certIdList) < 2:
        raise RuntimeError("证件号缺失，无法判断企业黑名单")
    blackResult = checkBlack(WDXT_Black, "BLACKLISTDATAS", certIdList)
    if blackResult == 0:
        return 0
    elif blackResult != 1:
        raise RuntimeError(blackResult)
    else:
        return 1


if __name__ == "__main__":
    WDXT_Black = {"sysHead": {"responseCode": "0"}, "body": {"BLACKLISTDATAS": [
        {"CERTTYPE": "Ent02", "CERTID": "3232"}
    ]}}
    WDXT_AmlBlack = {"sysHead": {"responseCode": "0"}, "body": {"AMLBLACKLISTDATAS": [
        {"CERTTYPE": "Ent02", "CERTID": "123456"}
    ]}}
    ratingRequest = {"legalPerson": u"朱为广","entTaxID" :"3232","legalPersonIdCard": "510102196706108511", "creditCode": "123456" ,"regNumber": "12345"}
    aa = getRule21()
    print aa

result = getRule21()

